﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PosHacker;
using PosHacker.PosException;
using System.Threading;
using PosHacker.Form;
using WebHacker;

namespace web_ctrl_test
{
    public partial class Form2 : Form
    {
        private POSHacker ph;
        private Dictionary<string, string> vTestNames;

        public Form2()
        {
            InitializeComponent();
            ph = POSHacker.GetInstance("Ge0090", "Pa88word");
        }
        private void Form2_Load(object sender, EventArgs e)
        {


            WebHacker.WebTools wt = new WebTools();

            //wt.HttpRequest(@"http://localhost:5207/",new RequestHeaders());

            WebHacker.HttpRequestDictionary hrd = new HttpRequestDictionary();

            hrd.Add("UserName", "900000");
            hrd.Add("Password", "000000");
            hrd.Add("RememberMe", "false");
            wt.SubmitFormData(@"http://localhost:5207/", hrd.ToString());

            hrd = new HttpRequestDictionary();
            hrd.Add("ReceiveDate","");
            wt.SubmitFormData(@"http://localhost:5207/GPPTP/PTPReceived", hrd.ToString());


            hrd = new HttpRequestDictionary();
            hrd.Add("ReceiveDate","");
            wt.SubmitFormData(@"http://localhost:5207/GPEEM/EEMReceived", hrd.ToString());

            
            //ReceiveDate:2012/03/13
            //http://localhost:5207/GPPTP/PTPReceived

            //this.webBrowser1.DocumentText = "a";
            //this.webBrowser1.Update();

            //this.comboBox1.Items.Add("asd");

            ////
            //this.labelLogin.Text = "Loading...";

            //new Thread(new ThreadStart(Init)).Start();

            this.Close();
        }

        private void Init() {

            try
            {

                ph.Login();

                this.GetTestNames();
            }
            catch (PosException e)
            {
                //this.Invoke();
                MessageBox.Show(e.Message);
            }
        
        }

        private void testLogin() {
            this.labelLogin.Text = "Login OK!!";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //POSHacker ph = new POSHacker();
            //dlLogin hh = new dlLogin();
            //this.labelLogin.Text = "Logining";
            //Thread td = new Thread(new ThreadStart(StepOne));
            //td.Start();
            ph.Login();
            
           // this.webBrowser1.Url = new Uri(ph.TestText());





//            this.webBrowser1.DocumentText =
//ph.TestText();
            this.webBrowser1.Update();

            //this.webBrowser1.Document.GetElementById("").SetAttribute;
            //this.comboBox1

        }

        private void StepOne()
        {
            try
            {
                ph.Login();
            }
            catch (Exception e)
            {
                //this.Invoke()
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(GetTestNames)).Start();
        }



        private void GetTestNames()
        {

            this.vTestNames =
            this.ph.GetTestNames();
            this.Invoke(new PosDelegate(SetTestNames));
        }

        private void SetTestNames() {
            this.comboBox1.Items.Clear();

            //Dictionary<string, string>.ValueCollection.Enumerator tmpEnumerator =
            //this.vTestNames.Values.GetEnumerator();
            Dictionary<string, string>.KeyCollection.Enumerator tmpEnumerator =
            this.vTestNames.Keys.GetEnumerator();
            while (tmpEnumerator.MoveNext())
            {
                this.comboBox1.Items.Add(tmpEnumerator.Current);
            }

            this.comboBox1.SelectedIndex = 0;

            //end init
            this.labelLogin.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //List<FormSetCourse> tmpList =
            //this.ph.GetMarkableList(this.textBox1.Text);

            //foreach(FormSetCourse a in tmpList){

            //    Console.WriteLine(a.ToString());
            //}


        }




        private string GetTime()//equal JS:new Date().getTime()
        {

            //MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();
            //sc.Language = "javascript";
            //object obj = sc.Eval("new Date().getTime();");

            Int64 retval = 0;
            DateTime st = new DateTime(1970, 1, 1);
            //DateTime st = DateTime.Now;
            TimeSpan t = (DateTime.Now.ToUniversalTime() - st);
            retval = (Int64)(t.TotalMilliseconds + 0.5);
            return retval.ToString();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }




    }
}
